package com.ruoyi.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * 跨域配置类
 * @Description:
 * @Version: 1.0
 * 2024/5/27 19:17
 */
@Configuration
public class MyWebConfigurer implements WebMvcConfigurer {

    @Value("${domimage.uploaddir}")
    private String uploaddir;

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;
    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String prexypath = minIOConfigProperties.getPrexypath();
        int indexOfDoubleSlash = prexypath.indexOf("//");
        int indexOfFirstSlashAfterHost = prexypath.indexOf("/", indexOfDoubleSlash + 2);
        String substring = prexypath.substring(indexOfFirstSlashAfterHost);
        registry.addResourceHandler(substring+"**").addResourceLocations("file:" + uploaddir);
    }

    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("http://192.168.2.18:8889")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*");
    }
 
}
 
 